package abc.com.abc.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import abc.com.abc.bean.MSGBean;
import abc.com.abc.bean.UserBean;


public class DBUtils {
    private static DBUtils instance = null;
    private static SQLiteHelper helper;
    public static SQLiteDatabase db;

    public DBUtils(Context context) {
        helper = new SQLiteHelper(context);
        db = helper.getWritableDatabase();
    }


    public static DBUtils getInstance(Context context) {
        if (instance == null) {
            instance = new DBUtils(context);
        }
        return instance;
    }

    /**
     * 保存个人信息
     */

    public void saveUserInfo(UserBean bean) {
        ContentValues cv = new ContentValues();
        cv.put("userName", bean.userName);
        cv.put("name", bean.name);
        cv.put("carModel", bean.carModel);
        cv.put("jiakao", bean.jiakao);
        cv.put("jiaolian", bean.jiaolian);
        cv.put("phone", bean.phone);
        db.insert(SQLiteHelper.U_USERINFO, null, cv);

    }


    /**
     * 获取个人资料
     *
     * @param userName
     * @return
     */
    public UserBean getUserInfo(String userName) {
        String sql = "SELECT * FROM " + SQLiteHelper.U_USERINFO + " WHERE userName = ? ";
        Cursor cursor = db.rawQuery(sql, new String[]{userName});
        UserBean bean = null;
        while (cursor.moveToNext()) {
            bean = new UserBean();
            bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
            bean.name = cursor.getString(cursor.getColumnIndex("name"));
            bean.carModel = cursor.getString(cursor.getColumnIndex("carModel"));
            bean.jiakao = cursor.getString(cursor.getColumnIndex("jiakao"));
            bean.jiaolian = cursor.getString(cursor.getColumnIndex("jiaolian"));
            bean.phone = cursor.getString(cursor.getColumnIndex("phone"));
        }

        cursor.close();
        return bean;
    }

    /**
     * 修改个人资料
     *
     * @param key
     * @param value
     * @param userName
     */
    public void updateUserInfo(String key, String value, String userName) {
        ContentValues cv = new ContentValues();
        cv.put(key, value);
        db.update(SQLiteHelper.U_USERINFO, cv, "userName=?", new String[]{userName});
    }
    public List<UserBean> getAllUserInfo() {
        String sql = "SELECT * FROM " + SQLiteHelper.U_USERINFO;
        Cursor cursor = db.rawQuery(sql, null);
        List<UserBean> msgList = new ArrayList<UserBean>();
        UserBean bean = null;
        while (cursor.moveToNext()) {
            bean = new UserBean();
            bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
            bean.name = cursor.getString(cursor.getColumnIndex("name"));
            bean.carModel = cursor.getString(cursor.getColumnIndex("carModel"));
            bean.jiakao = cursor.getString(cursor.getColumnIndex("jiakao"));
            bean.jiaolian = cursor.getString(cursor.getColumnIndex("jiaolian"));
            bean.phone = cursor.getString(cursor.getColumnIndex("phone"));
            msgList.add(bean);
        }
        return msgList;
    }
    public void saveMSG(MSGBean bean){
        ContentValues cv = new ContentValues();
        cv.put("userName",bean.userName);
        cv.put("receiver",bean.receiver);
        cv.put("msg",bean.msg);
        db.insert(SQLiteHelper.U_MSG,null,cv);
    }

    public List<MSGBean> getAllMsg() {
        String sql = "SELECT * FROM " + SQLiteHelper.U_MSG ;
        Cursor cursor = db.rawQuery(sql,null);
        List msgList = new ArrayList<MSGBean>();
        MSGBean bean = null;
        while (cursor.moveToNext()) {
            bean = new MSGBean();
            bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
            bean.receiver = cursor.getString(cursor.getColumnIndex("receiver"));
            bean.msg = cursor.getString(cursor.getColumnIndex("msg"));
            bean._id = cursor.getInt(cursor.getColumnIndex("_id"));
            msgList.add(bean);
        }

        cursor.close();
        return msgList;
    }



}
